package com.project.controller;

import com.project.bean.OutDrugBean;
import com.project.service.IOutService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

/**
 * @author RJ
 * @date 2022/9/16 10:23
 */
@RestController
@RequestMapping("out")
public class OutController {
    @Resource
    private IOutService outService;

    @RequestMapping("buy")
    public String buy(Integer drugId, HttpServletRequest request) {
        Map<Integer, OutDrugBean> map = (Map<Integer, OutDrugBean>) request.getSession().getAttribute("map");
        if (drugId != null) {
            outService.buy(drugId, map);
        }
        return "ok";
    }

    @RequestMapping("remove")
    public String remove(Integer drugId, HttpServletRequest request) {
        Map<Integer, OutDrugBean> map = (Map<Integer, OutDrugBean>) request.getSession().getAttribute("map");
        if (drugId != null) {
            outService.remove(drugId, map);
        }
        return "ok";
    }

    @RequestMapping("update")
    public String updateNum(Integer drugId, Integer newNum, HttpServletRequest request) {
        Map<Integer, OutDrugBean> map = (Map<Integer, OutDrugBean>) request.getSession().getAttribute("map");
        if (drugId != null) {
            outService.updateNum(drugId, map, newNum);
        }
        return "ok";
    }

    @RequestMapping("display")
    public Collection<OutDrugBean> display(HttpServletRequest request) {
        Map<Integer, OutDrugBean> map = (Map<Integer, OutDrugBean>) request.getSession().getAttribute("map");
        return map.values();
    }
}
